<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\common\controller\Backend;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    protected $noNeedRight = ['test'];

    public function test()
    {
        // 测试方法
        $adminModel = new Admin();

        $fuzhenModel = new \app\admin\model\Fuzhen();

        //查询导医的信息
        $list = $adminModel
            ->alias('a')
            ->join('fa_auth_group_access b', 'a.id = b.uid and b.group_id = 3')
            ->select();

        foreach ($list as &$v) {
            // $v['id'] 即为 导医的id

            // 查询 导医今日的已经回访数据
            $huifangNum = $fuzhenModel->whereTime('createtime', 'between', getDateString())->where('admin_id', $v['id'])->group('huifang_id')->count();

            $v['huifangNum'] = $huifangNum > -1 ? $huifangNum : 0;
        }

        dump($list);

    }


    /**
     * 查看
     */
    public function index()
    {

        $huifang = new \app\admin\model\Huifang();


        //总记录数
        $all_num = $huifang->count();
        $this->view->assign('all_num', $all_num);

        //今日需要回访
        $today_num = $huifang->whereTime('hftime', 'between', getDateString())->count();
        $this->view->assign('today_num', $today_num);

        //明日需要回访
        $tomorrow_num = $huifang->whereTime('hftime', 'between', getDateString(1))->count();
        $this->view->assign('tomorrow_num', $tomorrow_num);

        //未来7天需要回访
        $sever_num = $huifang->whereTime('hftime', 'between', getDateString(0, 6))->count();
        $this->view->assign('sever_num', $sever_num);

        //循环导医的数据,今日共回访和今日新添加的数据
        $listTodayData = $this->getHuifangTodayData();
        $this->view->assign('listTodayData', $listTodayData);


        return $this->view->fetch();
    }

    /*
     * 查看导医今日已经回访的数据
     * @return huifangNum  导医今日回访的数量
     * @return huifangNewAddNum  导医今日新添加的数量
     */
    protected function getHuifangTodayData()
    {
        $adminModel = new Admin();

        $fuzhenModel  = new \app\admin\model\Fuzhen();
        $huifangModel = new \app\admin\model\Huifang();


        //查询导医的信息
        $list = $adminModel
            ->alias('a')
            ->join('fa_auth_group_access b', 'a.id = b.uid and b.group_id = 3')
            ->select();

        foreach ($list as &$v) {
            // $v['id'] 即为 导医的id

            // 查询 导医今日的已经回访数据
            $huifangNum = $fuzhenModel->whereTime('createtime', 'between', getDateString())->where('admin_id', $v['id'])->group('huifang_id')->count();
            // 查询 导医今日新添加的数据
            $huifangNewAddNum = $huifangModel->whereTime('createtime', 'between', getDateString())->where('admin_id', $v['id'])->count();


            $v['huifangNum']       = $huifangNum > -1 ? $huifangNum : 0;
            $v['huifangNewAddNum'] = $huifangNewAddNum > -1 ? $huifangNewAddNum : 0;
        }

        return $list;
    }


}
